package com.bw.lib_mvvm

import android.os.Bundle
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ThreadUtils.runOnUiThread

/**
 *@ClassName BaseFragment
 *@Author hu..
 *@Data 2024/7/21
 *@Description
 */
abstract class BaseFragment <VDB: ViewDataBinding>: Fragment(){
    protected lateinit var binding: VDB

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater,bindLayout(),container,false)

        return binding.root
    }

    abstract fun bindLayout(): Int

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initView()
        initData()
        handleObserver()
    }

    abstract fun initView()

    abstract fun initData()

    open fun handleObserver(){

    }

    fun showMsg(msg:String){
        if(Looper.getMainLooper().thread != Thread.currentThread()){
            runOnUiThread{
                showMsgReal(msg)
            }
        }else{
            showMsgReal(msg)
        }
    }

    private fun showMsgReal(msg: String) {
        Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
    }
}